package com.quyu.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.quyu.dto.Result;
import com.quyu.entity.ShopType;
import com.quyu.mapper.ShopTypeMapper;
import com.quyu.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result queryTypeList() {
        String key = "cache:shop:type:list";
        String shopTypeListJson = stringRedisTemplate.opsForValue().get(key);
        if(StrUtil.isNotBlank(shopTypeListJson)){

           return Result.ok(JSONUtil.toList(shopTypeListJson,ShopType.class));
        }
        List<ShopType> shopTypeList = query().orderByAsc("sort").list();
        if(shopTypeList.isEmpty()){
            return Result.fail("店铺类型不存在");
        }
        stringRedisTemplate.opsForValue().set(key,JSONUtil.toJsonStr(shopTypeList));


        return Result.ok(shopTypeList);
    }
}
